<script lang="ts" setup>
import PageTable from "@/components/PageTable/index.vue";
import SearchHeader from "@/components/SearchHeader/index.vue";
import PageContainer from "@/components/PageContainer/index.vue";
import {reactive, ref} from "vue";
import {View, Edit, Remove, Promotion,DeleteFilled} from '@element-plus/icons-vue'
import EleOrderDetail from "@/views/maintenance/components/EleOrderDetail.vue";
import {ButtonColor} from "@/utils/buttonsColor";
import { ElMessageBox } from 'element-plus'
import { message } from "@/utils/message";
import {delOwnerEleOrder, delSendRecord, editMeter, ownerEleOrderList} from "@/api/demoApi";

defineOptions({
  name: "ownerEle"
});

/**-------------------搜索-------------------**/
//查询参数
const queryList = [
  {key:'projectNo',label:'电表号',type:'input'},
  {key:'projectName',label:'所属站点',type:'input'},
  {key:'projectName',label:'订单状态',type:'select',options:[
      {label:'待确认',value:0},
      {label:'待非标确认',value:1},
      {label:'待发起审批',value:2},
      {label:'已支付',value:3},
      {label:'审批驳回',value:4},
      {label:'审批撤回',value:5},
      {label:'已作废',value:6}
    ]},
]
//分页
const pageQuery = reactive({
  page:1,
  size:15
})
//查询回调
const searchHandler = (queryForm)=>{
  console.log(queryForm)
}
//分页回调
const paginationHandler = (pageInfo) =>{
  pageQuery.page = pageInfo.page
  pageQuery.size = pageInfo.size
}

/**-------------------列表-------------------**/
//总条数
const total = ref(0)
//表格列
const tableColumns = [
  {label:'电表号',key:'meterNo'},
  {label:'所属站点',key:'stationName'},
  {label:'所属项目',key:'projectName'},
  {label:'业主',key:'ownerName'},
  {label:'付款单位',key:'paymentName'},
  {label:'本次结算度数',key:'payDegree'},
  {label:'电费单价',key:'eleUnit'},
  {label:'应付总额',key:'eleTotal'},
  {label:'实际单价',key:'realUnit'},
  {label:'实付总额',key:'realTotal'},
  {label:'是否非标',key:'isTarget'},
  {label:'非标单价',key:'targetUnit'},
  {label:'非标总额',key:'targetTotal'},
  {label:'创建日期',key:'createTime'},
  {label:'订单状态',key:'status',custom:true},
  {label:'操作',key:'operation',custom:true,width: 300}
]
//表格行数据
const tableData = ref([])
const getTableData = ()=>{
  tableData.value = ownerEleOrderList()
}
getTableData()

/**-------------------操作-------------------**/
//弹窗
const eleOrderDetail = ref()
const openDialog = (type:string,row:object)=>{
  switch (type){
    case 'view':
      eleOrderDetail.value.dialogShow('订单详情',row)
      break
  }
}
//作废
const delHandler = async row =>{
  try{
    const result = await ElMessageBox.prompt('该操作将回退电表的业主已结算度数，请确认并填写作废原因', '订单作废',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        lockScroll:true,
        closeOnClickModal:false,
        closeOnPressEscape:false,
        inputPlaceholder: '请填写作废原因',
        inputValidator: (value)=>{
          if(!value) return '请输入作废原因'
          else return true
        },
        type: 'warning',
      }
    )
    //todo 删除接口
    if(result.action === 'confirm' && result.value) {
      row.status = 6
      row.cancelCase = result.value
      delOwnerEleOrder(row)
    }
    message('作废成功',{type:'success'})
    getTableData()
  }catch(err){

  }
}
//未完成提示
const notDone = ()=>{
  message('抱歉，功能设计中...',{type:'warning'})
}
</script>

<template>
  <PageContainer>
    <!--顶部筛选-->
    <SearchHeader :query-list="queryList" @search="searchHandler" />
    <!--    表格-->
    <PageTable :columns="tableColumns" :page="pageQuery.page" :size="pageQuery.size" :table-data="tableData" :total="total" @pagination="paginationHandler">
      <template #status="{row}">
        <el-tag v-if="row.status===0" type="info">待确认</el-tag>
        <el-tag v-if="row.status===1" type="info">待非标确认</el-tag>
        <el-tag v-if="row.status===2" type="primary">待发起审批</el-tag>
        <el-tag v-if="row.status===3" type="success">已支付</el-tag>
        <el-tag v-if="row.status===4" type="warning">审批驳回</el-tag>
        <el-tag v-if="row.status===5" type="warning">审批撤回</el-tag>
        <el-tag v-if="row.status===6" type="danger">已作废</el-tag>
      </template>
      <template #operation="{row}">
        <el-button :color="ButtonColor.VIEW" :icon="View" plain round size="small" @click="openDialog('view',row)">详情</el-button>
        <el-button v-if="row.status === 0" :icon="Promotion" plain round size="small" type="danger" @click="notDone">一键支付</el-button>
        <!--        <el-button v-if="[0,4,5].includes(row.status)" :icon="Mouse" plain round size="small" type="primary" @click="notDone">订单确认</el-button>-->
        <!--        <el-button v-if="row.status === 2" :icon="Promotion" plain round size="small" type="primary" @click="notDone">发起审批</el-button>-->
        <el-button v-if="[0,4,5].includes(row.status)" :icon="DeleteFilled" plain round size="small" type="danger" @click="delHandler(row)">订单作废</el-button>
      </template>
    </PageTable>

    <!--  弹窗-->
    <EleOrderDetail ref="eleOrderDetail" />
  </PageContainer>
</template>

<style lang="scss" scoped>

</style>
